﻿Public Class frmSingleUserProfile 
    Dim data As DataTable
    Private Sub SimpleButtonExit_Click(sender As Object, e As EventArgs) Handles SimpleButtonExit.Click
        Me.Close()
    End Sub

    Private Sub frmSingleUserProfile_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim mysql As New cMySQLDatabase
        Dim obj As Object
        obj = mysql.mysql_query("SELECT * FROM tb_user WHERE id_user='" & localUID & "' LIMIT 1")
        If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
            data = obj
            TextEditUsername.Text = data(0)("username")
            TextEditName.Text = data(0)("nama_user")
        End If
    End Sub

    Private Sub SimpleButtonSave_Click(sender As Object, e As EventArgs) Handles SimpleButtonSave.Click
        Dim mysql As New cMySQLDatabase
        Dim obj As Object
        If TextEditUsername.Text.Equals("") = False And TextEditName.Text.Equals("") = False And TextEditOldPassword.Text.Equals("") = False Then
            If TextEditNewPassword.Text.Equals("") = False Or TextEditCNewPassword.Text.Equals("") = False Then
                If TextEditNewPassword.Text.Equals(TextEditCNewPassword.Text) Then
                    obj = mysql.mysql_query("SELECT * FROM tb_user WHERE id_user='" & localUID & "' AND `password`=MD5('" & TextEditOldPassword.Text.Replace("'", "\'") & "') LIMIT 1")
                    If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                        data = obj
                        If data.Rows.Count > 0 Then
                            If mysql.mysql_non_query("UPDATE tb_user SET password=MD5('" & TextEditNewPassword.Text & "'), nama_user='" & TextEditName.Text & "' WHERE id_user='" & localUID & "'") Then
                                localNick = data(0)("nama_user")
                                MessageBox.Show("Data has been Updated")
                                Me.Close()
                            Else
                                MessageBox.Show("Failed to change user data")
                            End If
                        Else
                            MessageBox.Show("Invalid Old Password")
                        End If
                    Else
                        MessageBox.Show("Failed to change user data")
                    End If
                Else
                    MessageBox.Show("New Password and Confirmation Doesn't Match")
                End If
            Else
                obj = mysql.mysql_query("SELECT * FROM tb_user WHERE id_user='" & localUID & "' AND `password`=MD5('" & TextEditOldPassword.Text.Replace("'", "\'") & "') LIMIT 1")
                If Type.GetTypeCode(obj.GetType) <> TypeCode.Boolean Then
                    data = obj
                    If data.Rows.Count > 0 Then
                        If mysql.mysql_non_query("UPDATE tb_user SET nama_user='" & TextEditName.Text & "' WHERE id_user='" & localUID & "'") Then
                            localNick = data(0)("nama_user")
                            MessageBox.Show("Data has been Updated")
                            Me.Close()
                        Else
                            MessageBox.Show("Failed to change user data")
                        End If
                    Else
                        MessageBox.Show("Invalid Old Password")
                    End If
                Else
                    MessageBox.Show("Failed to change user data")
                End If
            End If
        End If
    End Sub
End Class